package com.gw.provider.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;

@RestController
public class ProviderController {
    @Autowired //注入queue
    private Queue queue;
    @Autowired //注入topic
    private Topic topic;
    @Autowired //注入spring封装的消息工具类
    private JmsMessagingTemplate jmsMessagingTemplate;
    //接收前端的请求，把数据发布到queue
    @RequestMapping("/sendQueue")
    public String sendQueue(String name){
        //把消息发送到指定队列
        sendMessage(queue,name);
        return "success";
    }
    //接收前端的请求，把数据发布到topic
    @RequestMapping("/sendTopic")
    public String sendTopic(String name){
        //把消息发送到指定队列
        sendMessage(topic,name);
        return "success";
    }
    //把消息发送到指定队列的方法
    public void sendMessage(Destination destination,String message){
        jmsMessagingTemplate.convertAndSend(destination,message);
    }
}
